/* 4p.h - Copyright (c) 2006 Helmar Wodtke */

#include <assert.h>
#include "config.h"
#define AN(x) assert((x) != NULL)
#define net(x) (x[3] + (x[2] << 8) + (x[1] << 16) + (x[0] << 24))

typedef unsigned char uc;
typedef long          cell;
typedef unsigned long ucell;
#ifndef UINT_BOMBS
typedef unsigned int  uint;
#endif

#ifndef CELLBITS
# define CELLBITS 32
#endif

typedef struct dict_
{
  void /*@shared@*/ /*@null@*/ *prv;
  char /*@shared@*/ /*@null@*/ *nam;
  void /*@shared@*/ /*@null@*/ *tok;
  void /*@shared@*/ /*@null@*/ *cls;
} dict_t;

#ifndef MPROTECT
#define myalloc      malloc
#define myfree       free
#endif
#define myrealloc    realloc
#define hash_new()   hash_new_(0)
typedef void **     hash_t;
/*@shared@*/ /*@null@*/ void *hash_lookup (hash_t h, void *s);
void *hash_lookup_i (hash_t h, void *s);
void hash_insert (hash_t h, void *s, void *info);
hash_t hash_new_ (/*@null@*/ hash_t h);

typedef struct context_t_
{
  /*@shared@*/ dict_t *word;
  /*@shared@*/ void *find;
  /*@shared@*/ /*@null@*/ dict_t *last;
  hash_t dict;
  void *prev;
} context_t;

extern context_t *last_context;

/* other things */

#ifdef LOWMEM
# define STACKSIZE     8 * 1024
# define DATASIZE      8 * 1024
# define CODESIZE      8 * 1024
#else
# define STACKSIZE     8 * 1024		/* cells */
# define DATASIZE     10 * 1024	* 1024  /* bytes */
# define CODESIZE     10 * 64 * 1024	/* cells */
#endif
#define LOOPSTACKSIZE 128	/* cells */
#define CTXSTACKSIZE  128	/* cells */
#define TIBSIZE       256

/* fixes */

#ifdef NO_ATOLL
# define atoll atol
#else
long long atoll(const char *s); /* tcc does not find it */
#endif

/* compile.c */

extern cell *cp,		/* compilation trace pointer         */
 *cpmax,			/* maximum compilation trace pointer */
 *cpmin,			/* minimum compilation trace pointer */
 *cpfrom;			/* current compilation starts here   */
extern cell stopmark;

void seal (void);
void compile_comma (cell a);
void compile (cell a);
void revoke_last (void);
int sequence (cell * p);
cell lastcall (void);
cell before (cell);

#define TRACE 32                /* how many commands are to trace? */

#define NATIVE(x) ((ucell)(x) < 1024)

#ifndef TRUE
# define TRUE (0 == 0)
#endif

#ifdef USE_DLOPEN
# include "dlopen_ffi.h"
#endif

void _m_mul(void);
void _um_mul(void);
void _um_mod(void);
void _sm_rem(void);
void _fm_mod(void);
void _ud_mod(void);
void _md_add(void);
void _md_mul(void);
void _dabs(void);

#ifndef ALIGNEMENT_FIX
# define GET(X) (*(cell*)(X))
# define SET(X,Y) (*(cell*)(X) = (cell)(Y))
#else
cell _get(unsigned char *p);
void _set(unsigned char *p, cell v);
# define GET(X) _get((void *)(X))
# define SET(X,Y) _set((void *)(X), (cell)(Y))
#endif

#ifndef SELF
# define SELF argv[0]
#endif


#ifdef SLEEPFIX
# define sleep(x)
# define usleep(x)
#endif
